package com.masterclient.app.businessmodule.vipmodule.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;

import butterknife.BindView;

/**
 * 公告详情
 */
public class WebViewActivity extends FCBusinessActivity {

    @BindView(R.id.progress)
    ProgressBar progress;
    @BindView(R.id.root_view)
    FrameLayout rootView;
    private WebView webview;
    private String url;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        String title = getIntent().getStringExtra("title");
        navigationBar.setTitle(title);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        url = getIntent().getStringExtra("url");
        setWebView();
    }

    private void setWebView() {
        rootView.removeAllViews();
        webview = new WebView(getApplicationContext());
        rootView.addView(webview);
        webview.setBackgroundColor(0);
        WebSettings webSettings = webview.getSettings();
        webSettings.setSupportZoom(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);//關鍵點
        webSettings.setLoadWithOverviewMode(true);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); //取消滚动条白边效果
        webview.setWebChromeClient(new WebChromC());
        webview.setWebViewClient(new SafeWebViewClient());
//        webview.setWebViewClient(new SafeWebViewClient());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webview.getSettings().setMixedContentMode(webview.getSettings()
                    .MIXED_CONTENT_ALWAYS_ALLOW);  //注意安卓5.0以上的权限
        }
        if(!TextUtils.isEmpty(url))
            webview.loadUrl(url);

    }


    public class SafeWebViewClient extends WebViewClient {

        /**
         * 当WebView得页面Scale值发生改变时回调
         */
        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            super.onScaleChanged(view, oldScale, newScale);
        }

        /**
         * 是否在 WebView 内加载页面
         *
         * @param view
         * @param url
         * @return
         */
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url == null) return false;
            try {
                if (!url.startsWith("http")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
            } catch (Exception e) { //防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
                return true;//没有安装该app时，返回true，表示拦截自定义链接，但不跳转，避免弹出上面的错误页面
            }
            //处理http和https开头的url
            view.loadUrl(url);
            return true;
        }

        /**
         * WebView 完成加载页面时回调，一次Frame加载对应一次回调
         *
         * @param view
         * @param url
         */
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

    }

    public class WebChromC extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            Activity activity = WebViewActivity.this;
            if (null != activity && !activity.isFinishing()) {
                if (newProgress == 100) {
                    progress.setVisibility(View.GONE);
                } else {
                    progress.setVisibility(View.VISIBLE);
                    progress.setProgress(newProgress);//设置加载进度
                }
            }
        }
    }

    protected void onDestroy() {
        webview.removeAllViews();
        webview.destroy();
        super.onDestroy();
    }


}
